package com.xiaoma.tpo.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaoma.tpo.dao.MkResultDao;
import com.xiaoma.tpo.dao.MkSingleAnswerDao;
import com.xiaoma.tpo.domain.MkResult;
import com.xiaoma.tpo.domain.MkSingleAnswer;
import com.xiaoma.tpo.domain.User;
import com.xiaoma.tpo.dto.MkListeningQuestionVo;
import com.xiaoma.tpo.dto.MkReadingQuestionVo;
import com.xiaoma.tpo.dto.MkSingleAnswerVo;
import com.xiaoma.tpo.dto.MockResultDto;
import com.xiaoma.tpo.utils.Pager;
@Service
public class MkSingleAnswerService {
	@Autowired
	private MkSingleAnswerDao mkSingleAnswerDao;
	
	@Autowired
	private MkResultDao mkResultDao;
	
	public Integer saveMkSingleAnswer(MkSingleAnswerVo mkSingleAnswerVo,Integer userId){
		int rId = 0;
		for(MkSingleAnswer mkSinlgeAnswer: mkSingleAnswerVo.getTestAnswer()){
			mkSinlgeAnswer.setUserId(userId);
			mkSinlgeAnswer.setMkTpoId(mkSingleAnswerVo.getTpoId());
			rId = mkSingleAnswerDao.insert(mkSinlgeAnswer);
		}
		return rId;
	}

	public Integer saveMkResult(MkSingleAnswerVo mkSingleAnswerVo,User user) {
		MkResult mkResult = new MkResult();
		mkResult.setUpdatedAt(new Date());
		mkResult.setMkTpoId(mkSingleAnswerVo.getTpoId());
		mkResult.setUserId(user.getId());
		mkResult.setType(mkSingleAnswerVo.getType());
		mkResult.setScore(mkSingleAnswerVo.getScore());
		mkResult.setRightCount(mkSingleAnswerVo.getRightCount());
		mkResult.setTotalCount(mkSingleAnswerVo.getTotalCount());
		Integer rId = mkResultDao.insert(mkResult);
		return rId;
	}

	public Pager findByUserIdAndTpoId(User user,
			MockResultDto resultDto) {
		Pager pager = mkSingleAnswerDao.findByUserIdAndTpoId(user,resultDto);
		
		return pager;
	}

	public Pager findListeningByUserIdAndTpoId(User user,
			MockResultDto resultDto) {
		Pager pager = mkSingleAnswerDao.findListeningByUserIdAndTpoId(user,resultDto);
		return pager;
	}

	public String findByUserAndTpo(MkSingleAnswerVo mkSingleAnswerVo, User user) {
		String str = mkSingleAnswerDao.findByUserAndTpo(mkSingleAnswerVo,user);
		return str;
	}
}
